Creating a world file and all about SDF in ROS 2

> #ros2-foxy     #gazebo-simulator

> In this blog, we will talk about world files in Gazebo, how to design a world through an image, why should you use an SDF file in ROS 2 as compared to an URDF file and ROS 2 launch files.

Author : Aryaman Shardul

Note : These instructions are for ROS 2 Foxy and Gazebo 11 and were tested on Ubuntu 20.4 LTS.

Table of Contents

What is a world file in Gazebo

How to design a world in Gazebo through an image

Interlinking of the various files

    
    <model name="ground">
    <pose>1 2.3 -.1 0 0 0</pose>
    <static>1</static>
    <link name="ground">
      <collision name="ground_coll">
        <geometry>
          <box>
            <size>10 10 .1</size>
          </box>
        </geometry>
        <surface>
          <contact>
            <ode/>
          </contact>
        </surface>
      </collision>
      <visual name="ground_vis">
        <geometry>
          <box>
            <size>10 10 .1</size>
          </box>
        </geometry>
        <material>
          <script>
            <uri>models/course.material</uri>
            <name>course</name>
          </script>
        </material>
      </visual>
    </link>
    </model> 

Designing the image

sra

redpath

sra_Gazebo

redpath_Gazebo

SDF in ROS 2

What is a SDF file

        
      <sdf version='1.7'>
          <model name='walle'>
            <link name='base_link'>
              <inertial>
                <pose>-0 0 -0.0025 0 -0 0</pose>
                <mass>0.00680000</mass>
                <inertia>
                  <ixx>0.000121426</ixx>
                  <ixy>1.28749e-19</ixy>
                  <ixz>2.00583e-22</ixz>
                  <iyy>0.000118984</iyy>
                  <iyz>-4.40398e-24</iyz>
                  <izz>0.000240029</izz>
                </inertia>
              </inertial>
              <collision name='base_link_collision'>
                <pose>0 0 0 0 -0 0</pose>
                <geometry>
                  <mesh>
                    <scale>1 1 1</scale>
                    <uri>meshes/base_link.STL</uri>
                  </mesh>
                </geometry>
              </collision>
              <collision name='base_link_fixed_joint_lump__camera_link_collision_1'>
                <pose>0.058 0 -0.015 0 -0 0</pose>
                <geometry>
                  <box>
                    <size>0.007 0.007 0.007</size>
                  </box>
                </geometry>
                <surface>
                  <contact>
                    <ode/>
                  </contact>
                  <friction>
                    <ode/>
                  </friction>
                </surface>
              </collision>     

Why use a SDF file in ROS 2

What are launch files

ROS 2 launch files

        
          def generate_launch_description():
            model_path = GazeboRosPaths.get_paths()

            env = {
                "GAZEBO_MODEL_PATH": model_path,
            }

            sdf_prefix = get_package_share_directory("my_bot")
            sdf_file = os.path.join(sdf_prefix, "urdf", "walle.sdf")

            world_prefix = get_package_share_directory("my_bot")
            world_file = os.path.join(world_prefix, "worlds", "sra.world")
    
            rviz_config_prefix = get_package_share_directory("my_bot")
            rviz_config_path = os.path.join(rviz_config_prefix, 'rviz/urdf_config.rviz')

      
      return LaunchDescription(
        [
            ExecuteProcess(
                cmd=[
                    "gazebo",
                    "-s",
                    "libgazebo_ros_init.so",
                    "-s",
                    "libgazebo_ros_factory.so",
                     world_file,
                ],
                output="screen",
                additional_env=env,
            ),
            Node(
                package="gazebo_ros",
                node_executable="spawn_entity.py",
                arguments=[
                    "-entity",
                    "walle",
                    "-x",
                    "-1",
                    "-y",
                    "0",
                    "-z",
                    ".41",
                    "-b",
                    "-file",
                    sdf_file,
                ],
            ),
            Node(
                package="robot_state_publisher",
                node_executable="robot_state_publisher",
                output="screen",
                arguments=[sdf_file],
            ),
            Node(
            package='rviz2',
            executable='rviz2',
            name='rviz2',
            output="screen",
            arguments=[rviz_config_path],
    )
        ]
    )

Conclusion

I hope you all have learnt something new from this blog and I encourage you all to try and create your own worlds in Gazebo using this method and launch your robots and worlds by creating your own launch files. Here is the link to my github repository so that you can go through the code and try it for yourselves.